	// AWTTextAreaPeer.cpp : implementation file
//

#include "stdafx.h"
#include "awt.h"
#include "AWTTextAreaPeer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(AWTTextAreaPeer, CEdit)

/////////////////////////////////////////////////////////////////////////////
// AWTTextAreaPeer

AWTTextAreaPeer::AWTTextAreaPeer(AWTComponent* comp)
{
	m_comp = comp;
	m_AWTWindow = NULL;
}

AWTTextAreaPeer::~AWTTextAreaPeer()
{
}


BEGIN_MESSAGE_MAP(AWTTextAreaPeer, CEdit)
	//{{AFX_MSG_MAP(AWTTextAreaPeer)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_CTLCOLOR_REFLECT()
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//AWTTextComponetPeer interface 
void AWTTextAreaPeer::getText(CString& text)
{
	GetWindowText(text);
}

void AWTTextAreaPeer::setText(CString& text)
{
	SetWindowText(text.GetBuffer(0));
}

void AWTTextAreaPeer::setEditable(BOOL b)
{
	SetReadOnly(!b);
}

int  AWTTextAreaPeer::getSelectionStart()
{
	int start, end;
	GetSel(start, end);
	return start;
}

int  AWTTextAreaPeer::getSelectionEnd()
{
	int start, end;
	GetSel(start, end);
	return end;
}

void AWTTextAreaPeer::select(int start, int end)
{
	SetSel(start, end);
}

void AWTTextAreaPeer::setCaretPosition(int pos)
{
	CPoint pt = PosFromChar(pos);
	SetCaretPos(pt);
}

int  AWTTextAreaPeer::getCaretPosition()
{
	CPoint pt = GetCaretPos();
  return CharFromPos(pt);
}

/////////////////////////////////////////////////////////////////////////////
// AWTTextAreaPeer message handlers

int AWTTextAreaPeer::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CEdit::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	m_AWTWindow = getAWTWindow(this);
/*
	CWnd* wnd = this;
	CString str;
	BOOL bFound = FALSE;
	do {
		wnd = wnd->GetParent();
		wnd->GetWindowText(str);
		if (str == "!~AWTWindow~!")
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTDialogPeer)))
		{
			bFound = TRUE;
			break;
		}
		else if (wnd->IsKindOf(RUNTIME_CLASS(AWTMdiChildWnd)))
		{
			bFound = TRUE;
			break;
		}
	} while (wnd != NULL);
	if (bFound)
		m_AWTWindow = wnd;		
*/	
	return 0;
}

void AWTTextAreaPeer::OnDestroy() 
{
	CEdit::OnDestroy();
	
	AWTWindowEvent e(m_comp, AWTWindowEvent::WINDOW_CLOSED);
//	m_comp->processWindowEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTWINDOWEVENT, 0, (LPARAM)&e);
}

HBRUSH AWTTextAreaPeer::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	AWTColor* fg = m_comp->getForeground();
	AWTColor* bk = m_comp->getBackground();
	CBrush* br = m_comp->getBkBrush();

	if (fg != NULL)
		pDC->SetTextColor(fg->getColorRef());
	if (bk != NULL)
		pDC->SetBkColor(bk->getColorRef());
	return (HBRUSH)*br;
}

void AWTTextAreaPeer::OnSetFocus(CWnd* pOldWnd) 
{
	CEdit::OnSetFocus(pOldWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_GAINED);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTTextAreaPeer::OnKillFocus(CWnd* pNewWnd) 
{
	CEdit::OnKillFocus(pNewWnd);
	
	AWTFocusEvent e(m_comp, AWTFocusEvent::FOCUS_LOST);
	m_comp->processFocusEvent(&e);
	if (m_AWTWindow != NULL)
		m_AWTWindow->SendMessage(WM_AWTFOCUSEVENT, 0, (LPARAM)&e);
}

void AWTTextAreaPeer::OnChange() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CEdit::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
	AWTTextEvent e(m_comp, AWTTextEvent::TEXT_VALUE_CHANGED);
	((AWTTextComponent*)m_comp)->processTextEvent(&e);
}
